<?php
set_time_limit(0);
$language_id = 1; // 发布的语言ID
/**
 * @
 * @Description:
 * @Copyright (C) 2013,All Rights Reserved.
 * -----------------------------------------------------------------------------
 * @author: chenjiazi (308759017@qq.com)
 * @Create: 2013-11-1
 * @Modify:
*/
include_once('includes/application_top.php');
//include_once ("connect.php");

$action = $_GET['action'];
$action = 'import';
if ($action == 'import') { //导入CSV
/*	$filename = $_FILES['file']['tmp_name'];
	if (empty ($filename)) {
		echo '请选择要导入的CSV文件！';
		 echo "<script>alert('请选择要导入的CSV文件！');history.back();</script>";
		exit;
	}*/
	$filename='size.csv';
	$handle = fopen($filename, 'r');
	$result = input_csv($handle); //解析csv
	$len_result = count($result);
	if($len_result==0){
		echo '没有任何数据！';
		exit;
	}
	

$n=count($arr)-1;
$com_num=0;
$news_count=0;

	for ($i = 1; $i < $len_result; $i++) { //循环获取各字段值
		$products_model = addslashes(trim($result[$i][0])); //中文转码
		$products_options_name = trim($result[$i][1]);
		$products_options_values_name = trim($result[$i][2]);
	
if(!empty($products_model) && !empty($products_options_name) && !empty($products_options_values_name)){ // start content
	
	
	  $get_product_id = $db->Execute("select products_id, products_model from " . TABLE_PRODUCTS . " where products_model = '" . $products_model . "'");
	  
  if ($get_product_id->RecordCount() > 0) {
      $products_id = $get_product_id->fields['products_id'];
    }	
		
	
if(!empty($products_model) && !empty($products_options_name) && !empty($products_options_values_name) && !empty($products_id)){// 自动添加属性



//	foreach ($products_options_name as $key => $value) {
	    
		//$array_name = explode('|', $key); // 自动属性集
		
		$array_name=array();  
		unset($array_name);
		
		//$array_name[1] = empty($array_name[1]) ? 0 :$array_name[1];
		$array_name[1] = 0; // 0代表下拉框
		print_r($array_name[1]);
		$options = $db -> Execute("select products_options_id
                                from " . TABLE_PRODUCTS_OPTIONS . "
                                where language_id = '" . (int)$language_id . "'
								and products_options_name = '" . addslashes($products_options_name) . "'
								and products_options_type = '" . $array_name[1] . "'
                                Limit 0,1");
													
echo "select products_options_id
                                from " . TABLE_PRODUCTS_OPTIONS . "
                                where language_id = '" . (int)$language_id . "'
								and products_options_name = '" . addslashes($products_options_name) . "'
								and products_options_type = '" . $array_name[1] . "'
                                Limit 0,1";								
		if ($options -> RecordCount() > 0) {
			$optid = $options -> fields['products_options_id'];
		} else {
			$max_options_id_values = $db -> Execute("select max(products_options_id) + 1 as next_id
                                             from " . TABLE_PRODUCTS_OPTIONS);
			$optid = $max_options_id_values -> fields['next_id']; if($optid==0){ $optid=1;}
		 $options = $db -> Execute("insert into " . TABLE_PRODUCTS_OPTIONS . " (`products_options_id`, `language_id`, `products_options_name`, `products_options_sort_order`, `products_options_type`, `products_options_length`, `products_options_comment`, `products_options_size`, `products_options_images_per_row`, `products_options_images_style`, `products_options_rows`) VALUES('" . $optid . "', '" . (int)$language_id . "', '" . addslashes($products_options_name) . "', '" . $optid . "', '" . $array_name[1] . "', 32, NULL, 32, 0, 0, 0);"); 
		} 
		$array_attr = explode('|', $products_options_values_name); // 添加属性值
		foreach (array_unique(array_filter($array_attr)) as $attr_key => $attr_value) {
			$check = $db -> Execute("select pov.products_options_values_id, pov.products_options_values_name, pov.language_id
                                from " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov
                                left join " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " pov2po on pov.products_options_values_id = pov2po.products_options_values_id
                                where pov.language_id= '" . (int)$language_id . "'
                                and pov.products_options_values_name='" . addslashes($attr_value) . "'
                                and pov2po.products_options_id ='" . (int)$optid . "'");
			if ($check -> fields['products_options_values_id']) {
				$db -> Execute("insert into " . TABLE_PRODUCTS_ATTRIBUTES . " (products_id, options_id, options_values_id, products_options_sort_order) values('" . $products_id . "', '" . $optid . "', '" . $check -> fields['products_options_values_id'] . "', '" . $check -> fields['products_options_values_id'] . "')");
			} else {
				$start_id = $db -> Execute("select pov.products_options_values_id from " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov order by pov.products_options_values_id DESC LIMIT 1");
				$next_id = ($start_id -> fields['products_options_values_id'] + 1);
				$db -> Execute("insert into " . TABLE_PRODUCTS_OPTIONS_VALUES . "
                      (products_options_values_id, language_id, products_options_values_name,products_options_values_sort_order)
                      values ('" . (int)$next_id . "',
                              '" . (int)$language_id . "',
                              '" . addslashes($attr_value) . "',
							  '" . (int)$next_id . "')");
				$db -> Execute("insert into " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . "
                    (products_options_id, products_options_values_id)
                    values ('" . (int)$optid . "', '" . (int)$next_id . "')");

				$db -> Execute("insert into " . TABLE_PRODUCTS_ATTRIBUTES . " (products_id, options_id, options_values_id, products_options_sort_order) values('" . $products_id . "', '" . $optid . "', '" . $next_id . "', '" . $next_id . "')"); 
			} 
		} // foreach end
		
		
	} // if  自动添加属性 end
	
	
} // end content
		
				
		
		
//}		
	} // for end
	
	$data_values = substr($data_values,0,-1); //去掉最后一个逗号
	fclose($handle); //关闭指针
	/*$query = mysql_query("insert into news_articles (news_article_name,news_article_text,news_date_published) values $data_values");//批量插入数据表中
	if($query){
		echo '导入成功！';
	}else{
		echo '导入失败！';
	}*/
} elseif ($action=='export') { //导出CSV
    $result = mysql_query("select * from news_articles_text");
    $str = "news_article_name,news_article_text,年龄\n";
    $str = iconv('utf-8','gb2312',$str);
    while($row=mysql_fetch_array($result)){
        $name = iconv('utf-8','gb2312',$row['news_article_name']);
        $sex = iconv('utf-8','gb2312',$row['news_article_text']);
    	$str .= $name.",".$sex.",".$row['age']."\n";
    }
    $filename = date('Ymd').'.csv';
    export_csv($filename,$str);
}
function input_csv($handle) {
	$out = array ();
	$n = 0;
	while ($data = fgetcsv($handle, 10000)) {
		$num = count($data);
		for ($i = 0; $i < $num; $i++) {
			$out[$n][$i] = $data[$i];
		}
		$n++;
	}
	return $out;
}

function export_csv($filename,$data) {
    header("Content-type:text/csv");
    header("Content-Disposition:attachment;filename=".$filename);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    echo $data;
}
?>
